//
//  ReadDataHelper.m
//  News
//
//  Created by lanou3g on 15/11/11.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "ReadDataHelper.h"
#import "AFHTTPRequestOperationManager.h"
#import "ReadModel.h"

@interface ReadDataHelper ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation ReadDataHelper

SingletonM(ReadDataHelper)

- (void)requestDataFromNetWorkWithUrl:(NSString *)url didFinished:(dataFinished)finish
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    [mgr GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 移除所有数据
        if ([url isEqualToString:kDragDownUrls_read]) {
            [self.dataArray removeAllObjects];
        }
        
        if (responseObject == nil) return ;
        
        NSArray *arr = responseObject[@"推荐"];
        for (NSDictionary *dict in arr) {
            ReadModel *model = [[ReadModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.dataArray addObject:model];
        }
        
        finish();
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
    
}



#pragma mark - 懒加载
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)readDataArray
{
    return [self.dataArray copy];
}

@end
